package com.example.travel_front.Utils;

import java.io.IOException;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtils {

    private static final OkHttpClient client = new OkHttpClient();
    private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

    // GET, POST, PUT, DELETE 方法

        /**
         * 发送同步GET请求
         * @param url 请求的URL
         * @return 响应的内容
         * @throws IOException 网络异常
         */
        public static String sendOkHttpGetRequest(String url) throws IOException {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            try (Response response = client.newCall(request).execute()) {
                if (response.isSuccessful()) {
                    return response.body().string();
                } else {
                    throw new IOException("Unexpected code " + response);
                }
            }
        }

        /**
         * 发送异步GET请求
         * @param url 请求的URL
         * @param callback 请求回调
         */
        public static void sendOkHttpGetRequestAsync(String url, okhttp3.Callback callback) {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(callback);
        }

        /**
         * 发送同步POST请求
         * @param url 请求的URL
         * @param json 请求体内容，JSON格式
         * @return 响应的内容
         * @throws IOException 网络异常
         */
        public static String sendOkHttpPostRequest(String url, String json) throws IOException {
            MediaType JSON = MediaType.get("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(json, JSON);
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            try (Response response = client.newCall(request).execute()) {
                if (response.isSuccessful()) {
                    return response.body().string();
                } else {
                    throw new IOException("Unexpected code " + response);
                }
            }
        }
    /**
     * 发送异步POST请求
     * @param url 请求的URL
     * @param json 请求体内容，JSON格式
     * @param callback 请求回调
     */
    public static void sendOkHttpPostRequestAsync(String url, String json, Callback callback) {
        RequestBody body = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }


    /**
     * 发送同步PUT请求
     * @param url 请求的URL
     * @param json 请求体内容，JSON格式
     * @return 响应的内容
     * @throws IOException 网络异常
     */
    public static String sendOkHttpPutRequest(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .url(url)
                .put(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                throw new IOException("Unexpected code " + response);
            }
        }
    }

    /**
     * 发送同步DELETE请求
     * @param url 请求的URL
     * @return 响应的内容
     * @throws IOException 网络异常
     */
    public static String sendOkHttpDeleteRequest(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .delete()
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                throw new IOException("Unexpected code " + response);
            }
        }
    }
}
